// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Avoid 'chrome' namespace
var isChrome = false; //On Chrome
if (typeof browser === "undefined" && typeof chrome === "object") {
	var browser = chrome; //On Chrome
	isChrome = true;
}

browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
	if (message && message.type == 'copy2clipboard') {
		copyToClipboard(message.text);
	}
}
);

function copyToClipboard(textToCopy) {
	// navigator clipboard 需要https等安全上下文
	navigator.clipboard.writeText(textToCopy)
		.then(() => {
			console.log("navigator.clipboard copy!");
		})
		.catch(() => {
			const ta = document.querySelector('#ta');
			ta.value = textToCopy;
			ta.select();
			if (document.execCommand('copy')) {
				document.execCommand('copy')
			}
			close();
			console.log('document.execCommand copy!')
		})
}

